package com.rajaev.dtu.tractor.infrastructure.config;

import com.gizwits.noti.noticlient.OhMyNotiClient;
import com.gizwits.noti.noticlient.OhMyNotiClientImpl;
import com.gizwits.noti.noticlient.bean.req.NotiReqPushEvents;
import com.gizwits.noti.noticlient.bean.req.body.AuthorizationData;
import com.gizwits.noti.noticlient.config.SnotiCallback;
import com.gizwits.noti.noticlient.config.SnotiConfig;
import com.gizwits.noti.noticlient.enums.ProtocolType;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MySnotiConfig {

    AuthorizationData authorizationData(SnotiConfiguration snotiConfiguration) {
        AuthorizationData authorizationData = new AuthorizationData()
                //默认为 Wi-Fi_GPRS, 可选 NB_IoT, LORA. 此配置让client自动根据协议类型发送对应的请求体
                .setProtocolType(ProtocolType.V2)
                .setAuth_id(snotiConfiguration.getAuthId())
                .setAuth_secret(snotiConfiguration.getAuthSecret())
                .setSubkey(snotiConfiguration.getSubKey())
                //订阅事件, 建议按照需求订阅
                .addEvents(NotiReqPushEvents.values())
                .setProduct_key(snotiConfiguration.getProductKey());
        return authorizationData;
    }

    @Bean
    public OhMyNotiClient snotiClient(SnotiConfiguration configuration) {
        OhMyNotiClient snotiClient = new OhMyNotiClientImpl();
        //设置snoti回调, 默认回调见SnotiCallback#identity
        snotiClient.setCallback(SnotiCallback.identity())

                //加载登陆信息
                .addLoginAuthorizes(authorizationData(configuration))
                //snoti配置, 初始化配置见SnotiConfig
                .setSnotiConfig(new SnotiConfig());
        return snotiClient;
    }
}
